#include <REGX52.H>

sbit SCL=P2^1;
sbit SDA=P2^0;

void I2C_Start()	//开始
{
	SDA=1;
	SCL=1;
	SDA=0;
	SCL=0;
}

void I2C_Stop()		//结束
{
	SDA=0;	
	SCL=1;
	SDA=1;
}

void I2C_SendByte(unsigned char Byte)	//发送一个字节
{
	unsigned char i;
	for(i=0;i<8;i++)
	{
		SDA=Byte&(0x80>>i);
		SCL=1;
		SCL=0;
	}
}

unsigned char I2C_ReceiveByte()		//接受一个字节
{
	unsigned char i,Byte=0x00;
	SDA=1;	//SDA置1时释放，此时主机完全不干预通信线
	for(i=0;i<8;i++)
	{
		SCL=1;
		if(SDA){Byte|=(0x80>>i);}
		SCL=0;
	}
	return Byte;
}

void I2C_SendAck(unsigned char AckBit)	//发送应答
{
	SDA=AckBit;
	SCL=1;
	SCL=0;
}

unsigned char I2C_ReceiveAck()	//接受应答
{
	unsigned char AckBit;
	SDA=1;
	SCL=1;
	AckBit=SDA;
	SCL=0;
	return AckBit;
}